package com.third.customer.model.dao.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.third.customer.model.dao.CustomerGroupDao;
import com.third.customer.model.dao.param.CustomerGroupParam;
import com.third.customer.model.mapper.CustomerGroupMapper;
import com.third.customer.model.tables.CustomerGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by hehuaichun on 2020/5/21.
 */
@Repository
public class CustomerGroupDaoImpl implements CustomerGroupDao {

    @Autowired
    private CustomerGroupMapper customerGroupMapper;

    @Override
    public Page<CustomerGroup> query(CustomerGroupParam param) {
        Page<CustomerGroup> page = new Page<>(param.getPage(), param.getLimit());
        page.addOrder(OrderItem.desc("id"));
        return new LambdaQueryChainWrapper<>(customerGroupMapper)
                .eq(CustomerGroup::getCompanyId, param.getCompanyId())
                .like(!StringUtils.isEmpty(param.getName()), CustomerGroup::getName, param.getName())
                .page(page);
    }

    @Override
    public Optional<CustomerGroup> getById(long companyId, Long groupId) {
        CustomerGroup group = new LambdaQueryChainWrapper<>(customerGroupMapper)
                .eq(CustomerGroup::getId, groupId)
                .eq(CustomerGroup::getCompanyId, companyId)
                .one();
        return Optional.ofNullable(group);
    }

    @Override
    public List<CustomerGroup> getByIds(long companyId, List<Long> groupIds) {
        if(CollectionUtils.isEmpty(groupIds)){
            return Lists.newArrayList();
        }
        return new LambdaQueryChainWrapper<>(customerGroupMapper)
                .eq(CustomerGroup::getCompanyId, companyId)
                .in(CustomerGroup::getId, groupIds)
                .list();
    }

    @Override
    public List<CustomerGroup> getByCompanyId(long companyId) {
        return new LambdaQueryChainWrapper<>(customerGroupMapper)
                .eq(CustomerGroup::getCompanyId, companyId)
                .list();
    }

    @Override
    public Optional<CustomerGroup> getByName(Long companyId, String groupName) {
        return new LambdaQueryChainWrapper<>(customerGroupMapper)
                .eq(CustomerGroup::getCompanyId, companyId)
                .eq(CustomerGroup::getName, groupName).list().stream().findFirst();
    }

    @Override
    public boolean insert(CustomerGroup group) {
        return customerGroupMapper.insert(group) == 1;
    }

    @Override
    public boolean update(CustomerGroup group) {
        return customerGroupMapper.updateById(group) == 1;
    }

    @Override
    public boolean delete(Long id) {
        return customerGroupMapper.deleteById(id) == 1;
    }
}
